package net.goutalk.glcs.module.erpModel.caseErpCustomer.controller;

import cn.hutool.core.bean.BeanUtil;
import net.goutalk.glcs.common.annotation.GlcsLog;
import net.goutalk.glcs.common.constant.GlobalConstant;
import net.goutalk.glcs.common.model.result.R;
import net.goutalk.glcs.module.erpModel.caseErpCustomer.dto.AddCaseErpCustomerFollowDto;
import net.goutalk.glcs.module.erpModel.caseErpCustomer.dto.UpdateCaseErpCustomerFollowDto;
import net.goutalk.glcs.module.erpModel.caseErpCustomer.entity.CaseErpCustomerFollow;
import net.goutalk.glcs.module.erpModel.caseErpCustomer.service.ICaseErpCustomerFollowService;
import net.goutalk.glcs.module.erpModel.caseErpLog.entity.CaseErpLog;
import net.goutalk.glcs.module.erpModel.caseErpLog.service.ICaseErpLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * <p>
 * 客户跟进【case_erp_customer_follow】 前端控制器
 * </p>
 *
 * @author hnyyzy
 * @since 2023-07-11
 */
@RestController
@RequestMapping(GlobalConstant.CASE_ERP_CUSTOMER+"/caseErpCustomerFollow")
@Api(value = GlobalConstant.CASE_ERP_CUSTOMER +"/caseErpCustomerFollow", tags = "客户跟进")
@AllArgsConstructor
public class CaseErpCustomerFollowController {

    private ICaseErpCustomerFollowService caseErpCustomerFollowService;

    private ICaseErpLogService caseErpLogService;

    @PostMapping
    @ApiOperation(value = "新增客户跟进信息")
    public R add(@Valid @RequestBody AddCaseErpCustomerFollowDto dto) {
        CaseErpCustomerFollow caseErpCustomerFollow = BeanUtil.toBean(dto, CaseErpCustomerFollow.class);
        caseErpCustomerFollowService.save(caseErpCustomerFollow);

        CaseErpLog log = new CaseErpLog();
        log.setKeyId(dto.getCustomerId());
        log.setCategoryId(0);
        log.setExecuteResultJson("新增客户跟进");
        caseErpLogService.save(log);

        return R.ok(true);
    }

    @PutMapping
    @ApiOperation(value = "修改客户跟进信息")
    public R update(@Valid @RequestBody UpdateCaseErpCustomerFollowDto dto) {
        CaseErpCustomerFollow caseErpCustomerFollow = BeanUtil.toBean(dto, CaseErpCustomerFollow.class);
        caseErpCustomerFollowService.updateById(caseErpCustomerFollow);

        CaseErpLog log = new CaseErpLog();
        log.setKeyId(dto.getCustomerId());
        log.setCategoryId(0);
        log.setExecuteResultJson("修改客户跟进");
        caseErpLogService.save(log);

        return R.ok(true);
    }

    @GetMapping(value = "/info")
    @ApiOperation(value = "根据id查询客户跟进信息")
    public R info(@RequestParam Long id) {
        CaseErpCustomerFollow caseErpCustomerFollow = caseErpCustomerFollowService.getById(id);
        if (caseErpCustomerFollow == null) {
            R.error("找不到此客户跟进信息！");
        }
        return R.ok(caseErpCustomerFollow);
    }

    @DeleteMapping
    @ApiOperation(value = "删除")
    @GlcsLog(value = "删除客户跟进信息")
    public R delete(@Valid @RequestBody Long id) {
        return R.ok(caseErpCustomerFollowService.removeById(id));
    }
}
